package com.masterclient.app.modules.aftersale;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.aftersale.AfterSaleDetailData;
import com.eoner.baselibrary.bean.aftersale.AfterSalePurchaseBtnData;
import com.eoner.baselibrary.bean.aftersale.ProductPromotionItemBean;
import com.eoner.baselibrary.bean.aftersale.PromotionItemBean;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.sobot.SobotManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.base.ProgressLoginDialog;
import com.masterclient.app.modules.aftersale.adapter.AfterSaleDetailInfoAdapter;
import com.masterclient.app.modules.aftersale.adapter.MaterialImageAdapter;
import com.masterclient.app.modules.aftersale.contract.AfterSalePurchaseDetailContract;
import com.masterclient.app.modules.aftersale.contract.AfterSalePurchaseDetailPresenter;
import com.masterclient.app.modules.goods.ImageGalleryDialog;
import com.masterclient.app.widget.PriceIntegralLayout;
import com.masterclient.app.widget.TitlebarView;
import com.gyf.immersionbar.ImmersionBar;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode;
import com.sobot.chat.api.model.Information;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hjian
 * @date 2020/9/28.
 */
@Route(path = AroutePath.AFTER_SALE_PURCHASE_DETAIL_ACTIVITY)
public class AfterSalePurchaseDetailActivity extends BaseActivity<AfterSalePurchaseDetailPresenter> implements AfterSalePurchaseDetailContract.View {

    @BindView(R.id.after_sale_detail_view)
    View afterSaleDetailView;
    @BindView(R.id.after_sale_detail_bar)
    TitlebarView afterSaleDetailBar;
    @BindView(R.id.after_sale_detail_refresh)
    SwipeRefreshLayout afterSaleDetailRefresh;
    @BindView(R.id.after_sale_detail_title)
    TextView afterSaleDetailTitle;
    @BindView(R.id.after_sale_detail_subtitle)
    TextView afterSaleDetailSubtitle;
    @BindView(R.id.after_sale_detail_desc_layout)
    LinearLayout afterSaleDetailDescLayout;
    @BindView(R.id.after_sale_detail_address_title)
    TextView afterSaleDetailAddressTitle;
    @BindView(R.id.after_sale_detail_address_name)
    TextView afterSaleDetailAddressName;
    @BindView(R.id.after_sale_detail_address_mobile)
    TextView afterSaleDetailAddressMobile;
    @BindView(R.id.after_sale_detail_address_detail)
    TextView afterSaleDetailAddressDetail;
    @BindView(R.id.after_sale_detail_address_layout)
    ConstraintLayout afterSaleDetailAddressLayout;
    @BindView(R.id.after_sale_detail_progress_btn)
    TextView afterSaleDetailProgressBtn;
    @BindView(R.id.sale_detail_progress_good_iv1)
    ImageView saleDetailProgressGoodIv1;
    @BindView(R.id.sale_detail_progress_good_iv2)
    ImageView saleDetailProgressGoodIv2;
    @BindView(R.id.sale_detail_progress_good_iv3)
    ImageView saleDetailProgressGoodIv3;
    @BindView(R.id.sale_detail_progress_good_iv4)
    ImageView saleDetailProgressGoodIv4;
    @BindView(R.id.sale_detail_progress_good_iv5)
    ImageView saleDetailProgressGoodIv5;
    @BindView(R.id.after_sale_detail_progress_good)
    LinearLayout afterSaleDetailProgressGood;
    @BindView(R.id.sale_detail_progress_money_text1)
    TextView saleDetailProgressMoneyText1;
    @BindView(R.id.sale_detail_progress_money_iv1)
    ImageView saleDetailProgressMoneyIv1;
    @BindView(R.id.sale_detail_progress_money_iv2)
    ImageView saleDetailProgressMoneyIv2;
    @BindView(R.id.sale_detail_progress_money_iv3)
    ImageView saleDetailProgressMoneyIv3;
    @BindView(R.id.after_sale_detail_progress_money)
    LinearLayout afterSaleDetailProgressMoney;
    @BindView(R.id.after_sale_detail_progress_layout)
    LinearLayout afterSaleDetailProgressLayout;
    @BindView(R.id.after_sale_detail_progress_line)
    View afterSaleDetailProgressLine;
    @BindView(R.id.after_sale_detail_product_list)
    LinearLayout afterSaleDetailProductList;
    @BindView(R.id.after_sale_detail_product_price)
    PriceIntegralLayout afterSaleDetailProductPrice;
    @BindView(R.id.after_sale_detail_product_layout)
    LinearLayout afterSaleDetailProductLayout;
    @BindView(R.id.after_sale_detail_other)
    RecyclerView afterSaleDetailOther;
    @BindView(R.id.after_sale_detail_image)
    RecyclerView afterSaleDetailImage;
    @BindView(R.id.after_sale_detail_image_layout)
    LinearLayout afterSaleDetailImageLayout;
    @BindView(R.id.after_sale_detail_cancel)
    TextView afterSaleDetailCancel;
    @BindView(R.id.after_sale_detail_logistic_edit)
    TextView afterSaleDetailLogisticEdit;
    @BindView(R.id.after_sale_detail_logistic_view)
    TextView afterSaleDetailLogisticView;
    @BindView(R.id.after_sale_detail_root)
    View afterSaleDetailRoot;

    @Autowired(name = "order_id")
    String orderId;
    @Autowired(name = "order_product_id")
    String orderProductId;
    @BindView(R.id.after_sale_detail_again)
    TextView afterSaleDetailAgain;
    @BindView(R.id.after_sale_detail_update)
    TextView afterSaleDetailUpdate;

    private AfterSaleDetailData detailData;
    private ImageView[] goodIvs, moneyIvs;
    private MaterialImageAdapter imageAdapter;
    private AfterSaleDetailInfoAdapter infoAdapter;
    private ImageGalleryDialog imageGallery;
    private ProgressLoginDialog progressLoginDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_sale_detail);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this).statusBarDarkFont(false).transparentStatusBar().statusBarView(afterSaleDetailView).init();

        progressLoginDialog = new ProgressLoginDialog(this);
        afterSaleDetailBar.setRightText("联系客服");
        afterSaleDetailBar.setRightTextColor(Color.parseColor("#ffffff"));
        afterSaleDetailBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                Information info = SobotManager.getBaseInfo();
                if (detailData != null) {
                    Map<String, String> customInfo = new HashMap<String, String>();
                    customInfo.put("aftersale_no", detailData.getRefund_no());
                    customInfo.put("aftersale_status", detailData.getStatus_progress().get(0) + "");
                    info.setCustomInfo(customInfo);
                    int totalFee = (int) Float.valueOf(detailData.getRefund_amount()).floatValue();
                    String productName = "";
                    String productImageeUrl = "";
                    if(detailData.getProducts().size() > 0) {
                        productName = detailData.getProducts().get(0).getName();
                        productImageeUrl = detailData.getProducts().get(0).getImage();
                    }
                    SobotManager.setOrderCardInfo(info,detailData.getRefund_no(),detailData.getStatus(),totalFee,"" + detailData.getProducts().size(),detailData.getCreate_at(),productName,productImageeUrl);
                }
                SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.Default, "");
                SobotApi.startSobotChat(AfterSalePurchaseDetailActivity.this, info);
            }
        });
        afterSaleDetailRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.afterSaleDetail(orderId,orderProductId);
            }
        });

        goodIvs = new ImageView[]{saleDetailProgressGoodIv1, saleDetailProgressGoodIv2, saleDetailProgressGoodIv3,
                saleDetailProgressGoodIv4, saleDetailProgressGoodIv5};
        moneyIvs = new ImageView[]{saleDetailProgressMoneyIv1, saleDetailProgressMoneyIv2, saleDetailProgressMoneyIv3};

        mPresenter.afterSaleDetail(orderId,orderProductId);
    }

    @OnClick({R.id.after_sale_detail_again, R.id.after_sale_detail_update, R.id.after_sale_detail_progress_btn,
            R.id.after_sale_detail_cancel, R.id.after_sale_detail_logistic_edit, R.id.after_sale_detail_logistic_view})
    public void onViewClicked(View view) {
        if (detailData == null) return;
        switch (view.getId()) {
            case R.id.after_sale_detail_progress_btn:
                if (detailData.getStatus_progress() != null && detailData.getStatus_progress().size() > 1) {
                    Intent intent = new Intent(mContext, ReturnProgressActivity.class);
                    intent.putExtra("list", (Serializable) detailData.getStatus_tip());
                    intent.putExtra("state", String.valueOf(detailData.getStatus_progress().get(1)));
                    startActivity(intent);
                }
                break;
            case R.id.after_sale_detail_again:
                mPresenter.getRefundBtn(4,detailData.getId(),progressLoginDialog);
                break;
            case R.id.after_sale_detail_update:
                mPresenter.getRefundBtn(2,detailData.getId(),progressLoginDialog);
                break;
            case R.id.after_sale_detail_cancel:
                mPresenter.getRefundBtn(3,detailData.getId(),progressLoginDialog);
                break;
            case R.id.after_sale_detail_logistic_edit:
                mPresenter.getRefundBtn(1,detailData.getId(),progressLoginDialog);
                break;
            case R.id.after_sale_detail_logistic_view:
                mPresenter.getRefundBtn(0,detailData.getId(),progressLoginDialog);
                break;
        }
    }

    @Override
    public void showAfterSaleInfo(AfterSaleDetailData detailData) {
        if (afterSaleDetailRefresh.isRefreshing()) afterSaleDetailRefresh.setRefreshing(false);
        this.detailData = detailData;

        afterSaleDetailBar.setTitle("1".equals(detailData.getRefund_type())||"3".equals(detailData.getRefund_type()) ? "退货退款详情" : "退款详情");

        //顶部数据
        afterSaleDetailTitle.setText(detailData.getTop_arr().getTitle());
        if (TextUtils.isEmpty(detailData.getTop_arr().getDescription())) {
            afterSaleDetailSubtitle.setVisibility(View.GONE);
        } else {
            afterSaleDetailSubtitle.setText(detailData.getTop_arr().getDescription());
            afterSaleDetailSubtitle.setVisibility(View.VISIBLE);
        }
        //半圆描述
        afterSaleDetailDescLayout.removeAllViews();
        if (detailData.getRemark_arr()!=null&&detailData.getRemark_arr().size()>0) {
            for (AfterSaleDetailData.RemarkArrBean remark : detailData.getRemark_arr()) {
                TextView textView = new TextView(this);
                textView.setPadding(0, ScreenUtils.dp2px(2), 0, ScreenUtils.dp2px(2));
                textView.setTextSize(12);
                textView.setLineSpacing(ScreenUtils.dp2px(3), 1);
                textView.setTextColor(Color.parseColor(remark.getColor()));
                textView.setIncludeFontPadding(false);
                textView.setText(remark.getText());
                afterSaleDetailDescLayout.addView(textView);
            }
            afterSaleDetailDescLayout.setVisibility(View.VISIBLE);
            afterSaleDetailProgressLine.setVisibility(View.VISIBLE);
        }else {
            afterSaleDetailDescLayout.setVisibility(View.GONE);
            afterSaleDetailProgressLine.setVisibility(View.GONE);
        }

        //地址
        if (detailData.getSeller_address() != null && !TextUtils.isEmpty(detailData.getSeller_address().getMobile())) {
            afterSaleDetailAddressTitle.setText(detailData.getSeller_address().getTitle());
            afterSaleDetailAddressName.setText(detailData.getSeller_address().getFull_name());
            afterSaleDetailAddressMobile.setText(detailData.getSeller_address().getMobile());
            afterSaleDetailAddressDetail.setText(detailData.getSeller_address().getAddress());
            afterSaleDetailAddressLayout.setVisibility(View.VISIBLE);
        } else {
            afterSaleDetailAddressLayout.setVisibility(View.GONE);
        }

        //售后进度详情
        if (detailData.getStatus_progress() != null && detailData.getStatus_progress().size() > 1) {
            afterSaleDetailProgressLayout.setVisibility(View.VISIBLE);
            int index = detailData.getStatus_progress().get(0);
            int type = detailData.getStatus_progress().get(1);
            if ("1".equals(detailData.getRefund_type())) {
                afterSaleDetailProgressGood.setVisibility(View.VISIBLE);
                afterSaleDetailProgressMoney.setVisibility(View.GONE);
                for (int i = 0; i <= index; i++) {
                    goodIvs[i].setImageResource(R.mipmap.icon_dealer_aftersale_done);
                    if (i == index && type == 0) {
                        goodIvs[i].setImageResource(R.mipmap.icon_dealer_aftersale_error);
                    }
                }
                    for (int i = index+1; i < goodIvs.length; i++) {
                        goodIvs[i].setImageResource(R.mipmap.icon_dealer_aftersale_undone);
                    }

            } else {
                afterSaleDetailProgressGood.setVisibility(View.GONE);
                afterSaleDetailProgressMoney.setVisibility(View.VISIBLE);
                if ("3".equals(detailData.getRefund_type())){
                    saleDetailProgressMoneyText1.setText("申请退货");
                }
                for (int i = 0; i <= index; i++) {
                    moneyIvs[i].setImageResource(R.mipmap.icon_dealer_aftersale_done);
                    if (i == index && type == 0) {
                        moneyIvs[i].setImageResource(R.mipmap.icon_dealer_aftersale_error);
                    }
                }

                for (int i = index+1; i < moneyIvs.length; i++) {
                    moneyIvs[i].setImageResource(R.mipmap.icon_dealer_aftersale_undone);
                }
            }
        } else {
            afterSaleDetailProgressLayout.setVisibility(View.GONE);
        }

        //商品列表
        afterSaleDetailProductList.removeAllViews();
        int productSize = detailData.getProducts().size();
        if (detailData.getProducts() != null && productSize > 0) {
            afterSaleDetailProductPrice.setData("0", detailData.getRefund_amount());
            afterSaleDetailProductLayout.setVisibility(View.VISIBLE);
            for (int i = 0; i < productSize; i++) {
                ProductPromotionItemBean bean = detailData.getProducts().get(i);
                View view = View.inflate(mContext, R.layout.item_aftersale_goods, null);

                ViewTransformUtil.glideImageView(this, bean.getImage(),(ImageView) view.findViewById(R.id.iv_good),
                        new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0), R.mipmap.defaults,
                        ScreenUtils.dp2px(107), ScreenUtils.dp2px(107));

                view.findViewById(R.id.view_line).setVisibility(i==productSize-1?View.GONE:View.VISIBLE);
                ((TextView) view.findViewById(R.id.tv_title)).setText(bean.getName());
//                ((TextView) view.findViewById(R.id.tv_gos_num)).setVisibility(View.INVISIBLE);
                ((TextView) view.findViewById(R.id.tv_return_money)).setVisibility(View.INVISIBLE);
                ((TextView) view.findViewById(R.id.tv_skus)).setText("规格：" + bean.getAttribute_desc());


                TextView goodsNum = ((TextView) view.findViewById(R.id.tv_gos_num));
                PriceIntegralLayout llPrice = view.findViewById(R.id.ll_price);
                llPrice.setData(bean.getIntegral(),bean.getAmount(),true);
                goodsNum.setText(new StringBuffer("x").append(bean.getQty_ordered()));

                //提货单
                TextView tvPickGoodNum = view.findViewById(R.id.tv_pick_good_num);
                if ("5".equals(detailData.getRefund_mode())){
                    tvPickGoodNum.setText(new StringBuffer("提货数量 ").append(bean.getQty_ordered()));
                    tvPickGoodNum.setVisibility(View.VISIBLE);
                    llPrice.setVisibility(View.GONE);
                    goodsNum.setVisibility(View.GONE);
                }else {
                    tvPickGoodNum.setVisibility(View.GONE);
                    llPrice.setVisibility(View.VISIBLE);
                    goodsNum.setVisibility(View.VISIBLE);
                }


                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                                .withString("id", bean.getParent_product_id())
                                .navigation();
                    }
                });


                //赠品列表
                List<PromotionItemBean> productMessageList = bean.getPromotion();
                View cross_border_layout = view.findViewById(R.id.cross_border_layout);
                if (productMessageList != null && productMessageList.size() > 0) {
                    cross_border_layout.setVisibility(View.VISIBLE);
                    LinearLayout ll_shop_present_container = (LinearLayout) view.findViewById(R.id.ll_shop_present_container);
                    ll_shop_present_container.removeAllViews();
                    for (PromotionItemBean promotionProductMessage : productMessageList) {
                        View view_present_goods = View.inflate(mContext, R.layout.item_present, null);
                        ImageView ivPresentGood = (ImageView) view_present_goods.findViewById(R.id.iv_good);
                        TextView ivPresentTitle = (TextView) view_present_goods.findViewById(R.id.tv_title);
                        TextView ivPresentSkus = (TextView) view_present_goods.findViewById(R.id.tv_skus);
                        TextView ivPresentPrice = (TextView) view_present_goods.findViewById(R.id.tv_price);
                        TextView ivPresentNum = (TextView) view_present_goods.findViewById(R.id.tv_num);

                        ivPresentTitle.setText(promotionProductMessage.getName());
                        ivPresentNum.setText("×" + promotionProductMessage.getQty());
                        ivPresentSkus.setText("规格：" + promotionProductMessage.getAttribute_desc());
                        ivPresentPrice.setText("¥" + promotionProductMessage.getPrice());
                        GlideUtil.displayRoundImage(mContext, promotionProductMessage.getProduct_img(), ivPresentGood, 7, R.mipmap.defaults);


                        view_present_goods.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                                        .withString("id", promotionProductMessage.getParent_product_id())
                                        .navigation();
                            }
                        });
                        ll_shop_present_container.addView(view_present_goods);
                    }
                } else {
                    cross_border_layout.setVisibility(View.GONE);
                }

                afterSaleDetailProductList.addView(view);
            }
        } else {
            afterSaleDetailProductLayout.setVisibility(View.GONE);
        }

        //其他信息
        if (detailData.getContent_arr() != null && detailData.getContent_arr().size() > 0) {
            afterSaleDetailOther.setVisibility(View.VISIBLE);
            if (infoAdapter == null) {
                afterSaleDetailOther.setLayoutManager(new LinearLayoutManager(this));
                infoAdapter = new AfterSaleDetailInfoAdapter();
                infoAdapter.bindToRecyclerView(afterSaleDetailOther);
            }
            infoAdapter.setNewData(detailData.getContent_arr());
        } else {
            afterSaleDetailOther.setVisibility(View.GONE);
        }


        //图片凭证
        if (detailData.getImages() != null && detailData.getImages().size() > 0) {
            if (imageAdapter == null) {
                GridLayoutManager layoutManager = new GridLayoutManager(mContext, 4);
                afterSaleDetailImage.setLayoutManager(layoutManager);
                afterSaleDetailImage.addItemDecoration(new RecyclerView.ItemDecoration() {
                    @Override
                    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                        super.getItemOffsets(outRect, view, parent, state);
                        outRect.set(0, ScreenUtils.dp2px(13), ScreenUtils.dp2px(13), 0);
                    }
                });
                imageAdapter = new MaterialImageAdapter((ScreenUtils.getScreenWidth() -
                        ScreenUtils.dp2px(71)) / 4, 4);
                imageAdapter.bindToRecyclerView(afterSaleDetailImage);
                imageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (imageGallery == null) {
                            imageGallery = new ImageGalleryDialog(mContext, detailData.getImages());
                        }
                        imageGallery.show();
                        imageGallery.setCurrentIndex(position);
                    }
                });
            }
            imageAdapter.setNewData(detailData.getImages());
            afterSaleDetailImageLayout.setVisibility(View.VISIBLE);
        } else {
            afterSaleDetailImageLayout.setVisibility(View.GONE);
        }

        //按钮显示
        showButton();

        afterSaleDetailRoot.setVisibility(View.VISIBLE);
    }

    //按钮显示
    private void showButton(){
        if (detailData.getButton_arr()!=null) {
            //重新发起售后
            afterSaleDetailAgain.setVisibility(!TextUtils.isEmpty(detailData.getButton_arr().getAgain_apply_btn())&&
                    "1".equals(detailData.getButton_arr().getAgain_apply_btn())?View.VISIBLE:View.GONE);
            //修改申请
            afterSaleDetailUpdate.setVisibility(!TextUtils.isEmpty(detailData.getButton_arr().getUpdate_btn())&&
                    "1".equals(detailData.getButton_arr().getUpdate_btn())?View.VISIBLE:View.GONE);
            //撤销申请
            afterSaleDetailCancel.setVisibility(!TextUtils.isEmpty(detailData.getButton_arr().getCancel_btn())&&
                    "1".equals(detailData.getButton_arr().getCancel_btn())?View.VISIBLE:View.GONE);
            //填写退货物流
            afterSaleDetailLogisticEdit.setVisibility(!TextUtils.isEmpty(detailData.getButton_arr().getSet_shipment_btn())&&
                    "1".equals(detailData.getButton_arr().getSet_shipment_btn())?View.VISIBLE:View.GONE);
            //查看物流
            afterSaleDetailLogisticView.setVisibility(!TextUtils.isEmpty(detailData.getButton_arr().getView_shipment_btn())&&
                    "1".equals(detailData.getButton_arr().getView_shipment_btn())?View.VISIBLE:View.GONE);
        }
    }

    /**
     * @param type 0查看物流，1填写物流，2修改申请，3撤销申请，4重新申请
     * @param btnData
     */
    @Override
    public void showRefundBtn(int type, AfterSalePurchaseBtnData btnData) {
        progressLoginDialog.dismiss();
        switch (type){
            case 0:
                if ("1".equals(btnData.getButton().getView_shipment_btn())) {
                    startActivitry(LogistInfoActivity.class, new String[][]{{"order_id", detailData.getId()},{"track_no", ""},{"biz_type", "2"}});
                } else {
                    ToastUtil.showToast(btnData.getTips());
                }
                break;
            case 1:
                if ("1".equals(btnData.getButton().getSet_shipment_btn())) {
                    startActivitry(AddReturnLogistActivity.class, new String[][]{{"refund_id", detailData.getId()},{"type","purchase_after_sale"},
                            {"shipment_company", detailData.getShipment_company()}, {"shipment_code", detailData.getShipment_code()},
                            {"track_no", detailData.getTrack_no()}, {"update", !TextUtils.isEmpty(detailData.getTrack_no())?"1":"0"}});
                } else {
                    ToastUtil.showToast(btnData.getTips());
                }
                break;
            case 3:
                ConfirmDialog dialog = new ConfirmDialog(mContext, "确定要撤销售后申请？", "取消", "确定","#FF6809");
                dialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                    @Override
                    public void cancel() {
                        dialog.dismiss();
                    }

                    @Override
                    public void confirm() {
                        if ("1".equals(btnData.getButton().getCancel_btn())) {
                            mPresenter.closeAfterSale(detailData.getId());
                        } else {
                            ToastUtil.showToast(btnData.getTips());
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case 2:
                if ("1".equals(btnData.getButton().getUpdate_btn())) {
                    intentAfterSale();
                } else {
                    ToastUtil.showToast(btnData.getTips());
                }
                break;
            case 4:
                if ("1".equals(btnData.getButton().getAgain_apply_btn())) {
                    intentAfterSale();
                } else {
                    ToastUtil.showToast(btnData.getTips());
                }
                break;
        }
    }

    @Override
    public void showResult(int type, boolean isSuccess) {
        if (type == 0){
            if (afterSaleDetailRefresh.isRefreshing()) afterSaleDetailRefresh.setRefreshing(false);
        }
    }

    private void intentAfterSale(){
        List<Integer> refundTypeArr = detailData.getUpdate_refund_type_arr();
        if (refundTypeArr == null) return;
        if (refundTypeArr.size()==1){
            if (refundTypeArr.get(0) == 0){
                startActivitry(ReturnMoneyActivity.class, new String[][]{{"product_id", orderProductId}, {"from_type","purchase"}, {"order_id", orderId}, {"update", "1"}});
            }else if (refundTypeArr.get(0) == 2){
                ARouter.getInstance().build(AroutePath.RETURN_REDELIVER_ACTIVITY)
                        .withString("product_id", orderProductId)
                        .withString("update", "1")
                        .withString("from_type","purchase")
                        .withString("order_id", orderId).navigation();
            }else if (refundTypeArr.get(0) == 3){
                startActivitry(ReturnGoodsActivity.class, new String[][]{{"product_id", orderProductId}, {"order_id", orderId},
                        {"update", "1"},{"from_type","purchase"},{"refund_type","dispatched"}});
            }
        }else {
            Intent intent = new Intent(this, ChooseAfterSaleActivity.class);
            intent.putExtra("product_id", orderProductId);
            intent.putExtra("order_id", orderId);
            intent.putExtra("update", "1");
            intent.putExtra("from_type","purchase");
            intent.putExtra("show_list",(ArrayList<Integer>) refundTypeArr);
            startActivity(intent);
        }
    }
}
